<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Devlism {
	protected $config;
	protected $dl_object = array();
	
	function __construct(){
	    GLOBAL $config;
	    $this->config = $config;
	    $this->autoload();
	}
	
	function __destruct(){
	    
	}
	
	function load($type='' , $file='' , $name='' , $db_use='')
	{
	    $type = strtolower($type);
	    switch($type){
		case 'model':
		case 'models':
		    $this->load_model($file,$name,$db_use);
		    break;
		case 'view':
		case 'views':
		    $this->load_view($file,$name);
		    break;
		case 'library':
		case 'libraries':
		    $this->load_library($file,$name);
		    break;
		case 'helper':
		case 'helpers':
		    $this->load_helper($file);
		    break;
		case 'lang':
		case 'language':
		case 'languages':
		    $this->load_lang($file);
		    break;
		default:
		    $this->error("Error : Type $type is incorrect!");
	    }
	}
	
	private function load_model($file='' , $name='' , $db_use='')
	{
	    $temp = explode('/',$file);
	    $class = end($temp);
	    $lower_class = strtolower($class);
	    
	    if($name===''){
		$name = $lower_class;
	    }
	    
	    if(array_key_exists('model', $this->dl_object)){
		if (in_array($name, $this->dl_object['model'], TRUE)){
		    return;
		}
	    }else{
		$this->dl_object['model'] = array();
	    }
	    
	    if (isset($this->$name)){
		$this->error('The model name you are loading is the name of a resource that is already being used: '.$name);
	    }
	    
	    if(is_file(BASEPATH . '/modules/' . $this->module . '/models/' . $file . '.php')){ // หาถายใน controller ที่เรียก
		include BASEPATH . '/modules/' . $this->module . '/models/' . $file . '.php';
		
		$this->$name = new Registry;
		$this->$name = new $class($db_use);
		$this->dl_object['model'][] = $name;
		return;
	    }
	    
	    $module = $temp['0'];
	    array_shift($temp);
	    array_pop($temp);
	    $folder = implode('/',$temp);
	    $folder = trim($folder,'/');
	    if($folder!=''){
		$folder .= '/';
	    }
	    
	    if(is_file(BASEPATH . '/modules/' . $module . '/models/' . $folder . $class . '.php')){ // เช็คใน  modules อื่นๆ
		include BASEPATH . '/modules/' . $module . '/models/' . $folder . $class . '.php';
		$this->$name = new Registry;
		$this->$name = new $class($db_use);
		$this->dl_object['model'][] = $name;
	    }else{
		$this->error("Error : Model {$file}.php does not exist!");
	    }
	}
	
	private function load_view($file_path='' , $name='')
	{
	    $ext = 'tpl';
	    $temp = explode('/',$file_path);
	    $file = end($temp);
	    if(strpos($file,'.')>0){
		list($file,$ext) = explode('.',$file);
	    }
	    
	    array_pop($temp);
	    $folder = implode('/',$temp);
	    $folder = trim($folder,'/');
	    if($folder!=''){
		$folder .= '/';
	    }
	    
	    if($name===''){
		$name = 'view';
	    }
	    
	    if(array_key_exists('view', $this->dl_object)){
		if (in_array($name, $this->dl_object['view'], TRUE)){
		    unset($this->$name);
		}
	    }else{
		$this->dl_object['view'] = array();
	    }
	    
	    if (isset($this->$name)){
		$this->error('The view name you are loading is the name of a resource that is already being used: '.$name);
	    }
	    
	    if(is_file(BASEPATH . '/modules/' . $this->module . '/views/' . $folder . $file . '.' . $ext)){ // ค้นภายใน modules ตัวเอง
		$this->$name = new Registry;
		$this->$name = new DL_View('modules/' . $this->module . '/views/' . $folder , $file , $ext);
		$this->dl_object['view'][] = $name;
		return;
	    }
	    
	    $module = $temp['0'];
	    array_shift($temp);
	    $folder_ = implode('/',$temp);
	    $folder_ = trim($folder_,'/');
	    if($folder_!=''){
		$folder_ .= '/';
	    }
	    
	    if(is_file(BASEPATH . '/modules/' . $module . '/views/' . $folder_ . $file . '.' . $ext)){ // ค้นภายในโมดูลอื่น
		$this->$name = new Registry;
		$this->$name = new DL_View('modules/' . $module . '/views/' . $folder_ , $file , $ext);
		$this->dl_object['view'][] = $name;
		
	    }elseif(is_file(BASEPATH . '/' . $folder . $file . '.' . $ext)){ // หาที่  root site
		$this->$name = new Registry;
		$this->$name = new DL_View($folder , $file , $ext);
		$this->dl_object['view'][] = $name;
		
	    }else{
		$this->error("Error : View {$file_path} does not exist!");
	    }
	}
	
	private function load_library($file='' , $name='')
	{
	    $temp = explode('/',$file);
	    $class = end($temp);
	    $lower_class = strtolower($class);
	    
	    if($name===''){
		$name = $lower_class;
	    }
	    
	    if(array_key_exists('library', $this->dl_object)){
		if (in_array($name, $this->dl_object['library'], TRUE)){
		    return;
		}
	    }else{
		$this->dl_object['library'] = array();
	    }
	    
	    if (isset($this->$name)){
		$this->error('The library name you are loading is the name of a resource that is already being used: '.$name);
	    }
	    
	    if(is_file(BASEPATH . '/libraries/' . $file . '.php')){
		include BASEPATH . '/libraries/' . $file . '.php';
		
		$this->$name = new Registry;
		$this->$name = new $class;
		$this->dl_object['library'][] = $name;
		
	    }elseif(is_file(DLFMPATH . '/libraries/' . $file . '.php')){
		include DLFMPATH . '/libraries/' . $file . '.php';
		
		$this->$name = new Registry;
		$this->$name = new $class;
		$this->dl_object['library'][] = $name;
		
	    }else{
		$this->error("Error : library {$file}.php does not exist!");
	    }
	}
	
	private function load_helper($file_path='')
	{
	    if(is_file(BASEPATH . '/helpers/' . $file_path . '_helper.php')){
		include BASEPATH . '/helpers/' . $file_path . '_helper.php';
		
	    }elseif(is_file(DLFMPATH . '/helpers/' . $file_path . '_helper.php')){
		include DLFMPATH . '/helpers/' . $file_path . '_helper.php';
		
	    }else{
		$this->error("Error : helper {$file_path}_helper.php does not exist!");
	    }
	}
	
	private function load_lang($file_path='')
	{
	    if(is_file(BASEPATH . '/language/' . $file_path . '_lang.php')){
		include BASEPATH . '/language/' . $file_path . '_lang.php';
		
	    }else{
		$this->error("Error : language {$file_path}_lang.php does not exist!");
	    }
	    
	    if(isset($lang) AND is_array($lang)){
		if(!array_key_exists('lang', $this->dl_object)){
		    include DLFMPATH . '/core/DL_Lang.class.php';
		    $this->dl_object['lang'] = '';
		    $this->lang = new Registry;
		    $this->lang = new DL_Lang;
		    $this->lang->set_lang($lang);
		}else{
		    $this->lang->set_lang($lang);
		}
	    }
	}
	
	protected function error($msg=''){
	    echo $msg;
	    exit;
	}
	
	protected function autoload()
	{
	    GLOBAL $autoload;
	    
	    if(count($autoload['languages'])>0){
		$result = array_unique($autoload['languages']);
		foreach($result as $val){
		    $this->load_lang($val);
		}
	    }
	    
	    if(count($autoload['helpers'])>0){
		$result = array_unique($autoload['helpers']);
		foreach($result as $val){
		    $this->load_helper($val);
		}
	    }
	    
	    if(count($autoload['libraries'])>0){
		$result = array_unique($autoload['libraries']);
		foreach($result as $val){
		    $this->load_library($val);
		}
	    }
	}
    }

/* End of file Devlism.class.php */
/* Location: ./core/Devlism.class.php */